package hipro.country;

import java.lang.reflect.Type;
import java.util.ArrayList;

import org.ksoap2.serialization.SoapObject;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import hipro.Services;

public class Country {
	// name space for instance soup Object
	private String namespace = "";
	// URL of web service
	private String url = "";

	private Services services = null;

	public Country() {
		this.services = new Services();
	}

	public void setWebservice(String webservice) {
		this.namespace = webservice;
		this.url = webservice;
	}
	
	/**
	 * Get country by id
	 * @param username
	 * @param password
	 * @param countryId
	 * @return
	 */
	public hipro.entities.Country getCountry(String username, 
			String password, int countryId){
		// create object soap object with name space and method.
		SoapObject soapObject = new SoapObject(this.namespace, "getCountry");
		// set parameters for function in method that web service will call.
		soapObject.addProperty("username", username);
		soapObject.addProperty("password", password);
		soapObject.addProperty("countryId", countryId);

		String json = this.services.call(this.url, "getCountry", soapObject);

		hipro.entities.Country country = new hipro.entities.Country();
		Gson gson = new Gson();
		country =  gson.fromJson(json, hipro.entities.Country.class);

		return country;
	}
	
	/**
	 * Get list
	 * @param username
	 * @param password
	 * @return
	 */
	public ArrayList<hipro.entities.Country> getList(String username, 
			String password){
		// create object soap object with name space and method.
		SoapObject soapObject = new SoapObject(this.namespace, "getList");
		// set parameters for function in method that web service will call.
		soapObject.addProperty("username", username);
		soapObject.addProperty("password", password);

		String json = this.services.call(this.url, "getList", soapObject);

		ArrayList<hipro.entities.Country> arrList = new ArrayList<hipro.entities.Country>();
		Gson gson = new Gson();
		Type collectionType = new TypeToken<ArrayList<hipro.entities.Country>>(){}
			.getType();
		arrList =  gson.fromJson(json, collectionType);

		return arrList;
	}
}
